package edu.towson.cis.cosc442.project2.vendingmachine;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * The unit test Class for VendingMachine.
 */
public class VendingMachineTest {

	//sets the pretense for all the tests
		VendingMachine vendingmachine;
		@Before
	    
		public void setUp() throws Exception {
			 
	     }
		
		//Tests if the items are added correctly, if the correct pretenses are use
	    
	    @Test
	    public void AddItemTest() {

	            vendingmachine = new VendingMachine();

	            vendingmachine.addItem(new VendingMachineItem("Test Item 1", 1.0),
	                            VendingMachine.A_CODE);
	            vendingmachine.addItem(new VendingMachineItem("Test Item 2", 2.0),
	                            VendingMachine.B_CODE);
	            vendingmachine.addItem(new VendingMachineItem("Test Item 3", 3.0),
	                            VendingMachine.C_CODE);
	           
	    }
	    //tests that the machine will not add an already full slot
	    public void AddItemDuplicateTest() {
	        vendingmachine = new VendingMachine();

	        vendingmachine.addItem(new VendingMachineItem("Test Item 1", 1.0),
	                        VendingMachine.A_CODE);

	        vendingmachine.addItem(new VendingMachineItem("Test Item 1", 4.0),
	                        VendingMachine.A_CODE);
	}
	    
	    //Tests to make sure the items being deleted are empty after the deletion
	    @Test
	    public void RemoveItemTest() {
	            vendingmachine = new VendingMachine();

	            vendingmachine.addItem(new VendingMachineItem("Item Test 1", 1.0),
	                            VendingMachine.A_CODE);
	            vendingmachine.addItem(new VendingMachineItem("Item Test 2", 2.0),
	                            VendingMachine.B_CODE);
	            vendingmachine.addItem(new VendingMachineItem("Item Test 3", 3.0),
	                            VendingMachine.C_CODE);

	            //Deletes the items
	            vendingmachine.removeItem(VendingMachine.A_CODE);
	            vendingmachine.removeItem(VendingMachine.B_CODE);
	            
	            //Checks to make sure the slots are empty
	            assertTrue(vendingmachine.getItem(VendingMachine.A_CODE) == null);
	            assertTrue(vendingmachine.getItem(VendingMachine.B_CODE) == null);
	    }

	   
	    //Tests to make sure you can not delete an empty slot
	    @Test(expected = VendingMachineException.class)
	    public void RemoveItemEmptySlotTest() {
	            vendingmachine = new VendingMachine();

	            assertTrue(vendingmachine.getItem(VendingMachine.A_CODE) == null);
	            vendingmachine.removeItem(VendingMachine.A_CODE);
	    }

	   
	   //tests that you have inserted number as long as its greater than zero
	    //This can also be used to test the current balance as long as no purchases have been made
	    @Test
	    public void InsertMoneytEST() {
	            vendingmachine = new VendingMachine();
	            vendingmachine.insertMoney(2.0);
	            assertTrue(2.0 == vendingmachine.getBalance() );
	    }

	    //checks to make sure you can not enter in an amount less than zero
	    @Test(expected = VendingMachineException.class)
	    public void NegativeMoneyTest() {
	            vendingmachine = new VendingMachine();
	            vendingmachine.insertMoney(-1.0);
	            assertTrue(0 == vendingmachine.getBalance());
	    }
	    
	    
	    //Tests that you can make a purchase with money sufficient to the the amount
	    //Checks the balance before and after the purchase
	    @Test
	    public void PurchaseTest() {
	            vendingmachine = new VendingMachine();

	            vendingmachine.addItem(new VendingMachineItem("Kit Kat", 2.0),
	                            VendingMachine.A_CODE);
	            vendingmachine.addItem(new VendingMachineItem("watermelon", 1.0),
	                    VendingMachine.B_CODE);
	            vendingmachine.insertMoney(3);
	            assertTrue(3 == vendingmachine.getBalance());
	            assertTrue(vendingmachine.makePurchase(VendingMachine.A_CODE));
	            assertTrue(vendingmachine.makePurchase(VendingMachine.B_CODE));
	            assertTrue(0.0 == vendingmachine.getBalance());
	    }

	    //checks to see if you can not purchase an item with insufficient money
	    //made sure the test checked to see if you could buy one item first than not buy another
	    @Test
	    public void LessMoneyTest() {
	            vendingmachine = new VendingMachine();

	            vendingmachine.addItem(new VendingMachineItem("Butter Finger", 2.0),
	                            VendingMachine.A_CODE);
	            vendingmachine.addItem(new VendingMachineItem("Kit Kat", 2.0),
	                    VendingMachine.B_CODE);
	    
	            vendingmachine.insertMoney(3.0);

	            assertTrue(vendingmachine.makePurchase(VendingMachine.A_CODE));
	            assertFalse(vendingmachine.makePurchase(VendingMachine.B_CODE));
	    }

	    //checks to make sure the test is equal to the expected
	    @Test
	    public void ChangeTest() {
	            vendingmachine = new VendingMachine();

	            vendingmachine.addItem(new VendingMachineItem("Reese's", 1.0),
	                            VendingMachine.A_CODE);
	            vendingmachine.insertMoney(2.0);

	            vendingmachine.makePurchase(VendingMachine.A_CODE);
	            assertTrue(vendingmachine.returnChange()==1.0);
	    }
	    
	    
	    
	    //removes everything
	    @After
	    public void tearDown() {
	            vendingmachine = null;
	    }
}